import Vue from 'vue'
import Vuex from 'vuex'
import modules from './modules'
import { getOptions, getConfig } from '@/api/common'
import util from '@/utils/util'
Vue.use(Vuex)

const store = new Vuex.Store({
	modules,
	state: {
		allOptions: '',
		allConfig: ''
	},
	getters: {
		allOptions: state => state.allOptions ||
		 (util.isEmpty(uni.getStorageSync('allOptions')) ? {} : JSON.parse(uni.getStorageSync('allOptions'))),
		//  获取全局配置
		allConfig: state => state.allConfig
	},
	mutations: {
		// 全局选项
		setAllOptions(state, data = {}) {
			  state.allOptions = data || ''
			//   uni.setStorageSync('allOptions', JSON.stringify(data))
		},
		// 全局配置
		setAllConfig(state, data = {}) {
			  state.allConfig = data
			  uni.setStorageSync('allConfig', JSON.stringify(data))
		}
	  },
	actions: {
		// 获取全局选项
		getAllOptions({ commit }) {
		// sysBudgetStatus 预算   sysRenovationStatus 装修类型  sysBookingStatus 派单状态
			return getOptions({
				type: ''
			}).then((res) => {
				if (res.status === 'success') {
					commit('setAllOptions', res.data)
				} else {
					uni.showToast({
						title: res.errors.message,
						icon: 'none'
					})
				}
			})
		},
		// 获取全局配置
		getAllConfig({ commit }) {
			return getConfig().then((res) => {
				console.log(res.data)
				if (res.status === 'success') {
					commit('setAllConfig', res.data)
				} else {
					uni.showToast({
						title: res.errors.message,
						icon: 'none'
					})
				}
			})
		}
	}
})

export default store
